
import java.net.*;
import java.sql.Date;
import java.io.*;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * @author AMPM
 * 
 * This class represents a simple TCP client able to connect to
 * and communicate with a simple TCP server.
 *
 */

public class AliceClientOld {
        
	private static final String privateKey = "aaaaaaaa";
	
  public static void main (String args[]) throws Exception{
        // an object representing this computers IP address
    InetAddress serverAddress = InetAddress.getByName("localhost");
    int serverPortSara = 7896;
    int serverPortBob = 7897;
    int serverPortAlice = 7898;
    boolean mode = true;
    
    // we request to establish a socket connection to SaraServer
    Socket socket = new Socket( serverAddress, serverPortSara ); 
        
    // get the output stream from the socket
    OutputStream os = socket.getOutputStream();
    
    System.out.println("A: Requesting key from Sara..");
    requestKeyFromSara(mode,os);
    
    // get the input stream from the socket
    InputStream is = socket.getInputStream();
    // wrap it in the ObjectInputStream object
//    ObjectInputStream ois = new ObjectInputStream( is );
    ObjectInputStream ois = SymmetricCrypt.decrypt(is, privateKey);
    
    if ( mode = (Boolean) ois.readObject()) {
        long nonce = (Long) ois.readObject();

        String sender = (String) ois.readObject();
        System.out.println("Received message from " + sender);
        System.out.println("Nonce: " + nonce);
                
        String receiver = (String) ois.readObject();
        System.out.println("Receiver: " + receiver);
        
    } else {
//        String response = (String) ois.readObject(); 
//        System.out.println( response );
       
    }
        
    socket.close();
    
    socket = new Socket( serverAddress, serverPortBob );
    
    // get the output stream from the socket
         os = socket.getOutputStream();  
        
         sendTicketToBob(mode,os);
        
         // get the input stream from the socket
          is = socket.getInputStream();
         // wrap it in the ObjectOutputStream object
          ois = new ObjectInputStream( is );
        
          mode = (Boolean) ois.readObject();
          
          if (mode) {
                long nonce = (Long) ois.readObject();
                        String sender = (String) ois.readObject();
                        String receiver = (String) ois.readObject();
                        System.out.println(nonce);
                        System.out.println(sender);
                        System.out.println(receiver);
                
            } else {
                String response = (String) ois.readObject(); 
                System.out.println( response );
               
            }
                
          //String message = (String) ois.readObject();
          //System.out.println("Received by Alice: " + message);
                
                
                
    
   
        /*ServerSocket serverSocket = new ServerSocket( serverPortAlice );
    while ( true ) 
    {
        // the server socket starts listening for connections. When a connection
        // is requested from a client, a socket is returned
        
        socket = serverSocket.accept();
        
        // the client is handled by the ClientHandler object in a separate thread
        AliceClientHandler handler = new AliceClientHandler(socket);
        new Thread(handler).start();
        
        

    }
    
   */
    
    
    //socket = new Socket(serverAddress, serverPortBob);
    //os = socket.getOutputStream();
    
        //socket.close();
  }     
  
  public static long nonce(){
          return System.currentTimeMillis();
  
  }
  
  public static ObjectOutputStream requestKeyFromSara(boolean mode, OutputStream os) throws IOException {
            // wrap it in the ObjectOutputStream object
            ObjectOutputStream oos = new ObjectOutputStream( os );
            
            // the data for sending is written to the (wrapped) output stream
            oos.writeObject(mode);
            
            if (mode) {
                    oos.writeObject( nonce());
                    oos.writeObject( "A" ); // Sender
                    oos.writeObject( "B" ); // Receiver
                
            } else {
//                oos.writeObject( "Message 1" );
            }
            oos.flush();
          
          return oos;
  }
  
  public static ObjectOutputStream sendTicketToBob(boolean mode, OutputStream os) throws IOException {
            // wrap it in the ObjectOutputStream object
            ObjectOutputStream oos = new ObjectOutputStream( os );
            
            // the data for sending is written to the (wrapped) output stream
            oos.writeObject(mode);
            
            if (mode) {
                    oos.writeObject( nonce());
                    oos.writeObject( "A" );
                    oos.writeObject( "B" );
                
            } else {
//                oos.writeObject( "Message 3" );
            }
          
          return oos;
}
  

  
}
